﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlertSystem
{
    public sealed class AlertsAgregator
    {
        private Dictionary<Type, HashSet<IAlertItem>> _internalList = new Dictionary<Type, HashSet<IAlertItem>>();
        private object synchObj = new object();


        public void Add(IAlertItem item)
        {
            if (item == null)
                return;

            //lock (synchObj)
            {
                Type type = item.GetType();

                if (!_internalList.ContainsKey(item.GetType()))
                    _internalList.Add(type, new HashSet<IAlertItem>());

                var list = _internalList[type];

                list.Add(item);
            }
        }

        public IEnumerable<Type> AlertList
        {
            get { return _internalList.Keys; }
        }

        public IEnumerable<IAlertItem> GetAlertList(Type alertType)
        {
            if (_internalList.ContainsKey(alertType))
                return _internalList[alertType];

            return null;
        }

        public void Clear()
        {
            _internalList.Clear();
        }

        public void Clear(Type alertType)
        {
            if (!_internalList.ContainsKey(alertType))
                return;

            _internalList[alertType].Clear();
        }
    }
}
